package com.wtwd.campus.vcom.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.vcom.common.Constant;
import com.wtwd.campus.vcom.common.RespCode;
import com.wtwd.campus.vcom.common.RespEntity;
import com.wtwd.campus.vcom.common.ResponseEntity;
import com.wtwd.campus.vcom.entity.AddOrgPolygonFenceDto;
import com.wtwd.campus.vcom.service.ValidService;
import com.wtwd.campus.vcom.service.VcomFenceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/1/4 16:57
 * @Description: 围栏管理
 */
@Api(tags = "围栏模块")
@RestController
@RequestMapping("/vcom/fence")
public class VcomFenceController {

    private static final Logger logger = LoggerFactory.getLogger(VcomFenceController.class);

    @Autowired
    private ValidService validService;

    @Autowired
    private VcomFenceService vcomFenceService;

    /**
     * 添加圆形围栏（fenceId由调用者提供，如果已有，则全量替换）
     * 类似于添加围栏和imei的关联
     *
     * @param jsonObject
     * @param request
     * @return
     */
    @PostMapping("/addOrgCircleFence")
    public ResponseEntity addOrgCircleFence(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        logger.info("addOrgCircleFence param={}", jsonObject.toJSONString());
        JSONArray imeis = jsonObject.getJSONArray("imeis");
        List<String> imeiList = imeis.toJavaList(String.class);
        String token = request.getHeader(Constant.HEAD_TOKEN_KEY);
        Object validResult = validService.validImeiList(token, imeiList);
        if (validResult instanceof RespCode) {
            return new ResponseEntity((RespCode) validResult, false, null);
        }
        List<String> resultImeiList = (List<String>) validResult;
        jsonObject.remove("imeis");
        return vcomFenceService.addOriCircleFence(jsonObject, resultImeiList);
    }

    /**
     * 全量增加多边形围栏
     *
     * @param data
     * @return
     */
    @ApiOperation(value = "全量增加多边形围栏", notes = "")
    @PostMapping("/addOrgPolygonFence")
    public ResponseEntity addOrgPolygonFence(@RequestBody JSONObject data, HttpServletRequest request) {
        logger.info("addOrgPolygonFence param={}", data.toJSONString());
        String token = request.getHeader(Constant.HEAD_TOKEN_KEY);
        AddOrgPolygonFenceDto addOrgPolygonFenceDto = JSONObject.toJavaObject(data, AddOrgPolygonFenceDto.class);
        List<String> imeis = addOrgPolygonFenceDto.getImeis();
        Object validResult = validService.validImeiList(token, imeis);
        if (validResult instanceof RespCode) {
            return new ResponseEntity((RespCode) validResult, false, null);
        }
        List<String> resultImeiList = (List<String>) validResult;
        addOrgPolygonFenceDto.setImeis(resultImeiList);
        return vcomFenceService.addOrgPolygonFence(addOrgPolygonFenceDto);
    }


    public static void main(String[] args) {
        String s="{\"address\":\"岳巍的围栏\",\"fenceType\":2,\"latitude\":0.0,\"isActive\":true,\"fencePoints\":[{\"latitude\":34.75170488015928,\"longitude\":113.60957614427308,\"index\":1},{\"latitude\":34.75038548358815,\"longitude\":113.62522132943728,\"index\":2},{\"latitude\":34.74544976422546,\"longitude\":113.61891494214568,\"index\":3},{\"latitude\":34.74562163901729,\"longitude\":113.61000442348207,\"index\":4}],\"fenceName\":\"岳巍的围栏\",\"imeis\":[\"867696065993294\",\"867696065989839\",\"867696065994201\",\"867696065988807\",\"867696065987437\",\"867696065987361\",\"867696065987288\",\"867696065991710\",\"867696065988708\",\"867696065991215\",\"867696065997501\",\"867696065991819\",\"867696065987924\",\"867696065997907\",\"867696065998442\",\"867696065997261\",\"867696065989656\",\"867696065997428\",\"867696065997550\",\"867696065990969\",\"867696065992130\",\"867696065988989\",\"867696065996255\",\"867696065995315\",\"867696065998426\",\"867696065995182\",\"867696065987908\",\"867696065993179\",\"867696065991793\",\"867696065992221\",\"867696065987312\",\"867696065997790\",\"867696065987999\",\"867696065988443\",\"867696065990480\",\"867696065993781\",\"867696065990324\",\"867696065987536\",\"867696065995638\",\"867696065994631\",\"867696065991090\",\"867696065997733\",\"867696065987734\",\"867696065994656\",\"867696065997287\",\"867696065998509\",\"867696065997147\",\"867696065990936\",\"867696065987197\",\"867696065990779\",\"867696065989870\",\"867696065994359\",\"867696065997253\",\"867696065989276\",\"867696065992429\",\"867696065992916\",\"867696065989961\",\"867696065990373\",\"867696065993344\",\"867696065992882\",\"867696065995190\",\"867696065987775\",\"867696065996289\",\"867696065990183\",\"867696065996313\",\"867696065995513\",\"867696065988112\",\"867696065995349\",\"867696065988096\",\"867696065994441\",\"867696065995471\",\"867696065992379\",\"867696065992346\",\"867696065994417\",\"867696065994649\",\"867696065987650\",\"867696065997675\",\"867696065996222\",\"867696065995588\",\"867696065993708\",\"867696065988039\",\"867696065995737\",\"867696065996214\",\"867696065987346\",\"867696065996248\",\"867696065996198\",\"867696065995240\",\"867696065996131\",\"867696065996271\",\"867696065995760\",\"867696065987403\",\"867696065991082\",\"867696065994227\",\"867696065994540\",\"867696065988021\",\"867696065995935\",\"867696065996206\",\"867696065987767\",\"867696065997923\",\"867696065994995\",\"867696065307370\",\"867696065320779\",\"867696065314020\",\"867696065993807\",\"867696065322288\",\"867696065320738\",\"867696065306463\",\"867696065998160\",\"867696065322270\",\"867696065993252\",\"867696065987809\",\"867696065322254\",\"867696065314962\",\"867696065997329\",\"867696065988864\",\"867696065997840\",\"867696065994060\",\"867696065997725\",\"867696065989128\",\"867696065315225\",\"867696065992023\",\"867696065997113\",\"867696065998210\",\"867696065322130\",\"867696065321124\",\"867696065314087\",\"867696065990746\",\"867696065997824\",\"867696065998335\",\"867696065995679\",\"867696065988393\",\"867696065989946\",\"867696065994391\",\"867696065988310\",\"867696065995075\",\"867696065990514\",\"867696065997873\",\"867696065993864\",\"867696065991512\",\"867696065992866\",\"867696065992411\",\"867696065992122\",\"867696065998137\",\"867696065988781\",\"867696065993591\",\"867696065998376\",\"867696065989060\",\"867696065992189\",\"867696065988724\",\"867696065994730\",\"867696065994623\",\"867696065990761\",\"867696065989011\",\"867696065989458\",\"867696065991330\",\"867696065995356\",\"867696065993583\",\"867696065995547\",\"867696065994334\",\"867696065988815\",\"867696065987619\",\"867696065994797\",\"867696065994144\",\"867696065993120\",\"867696065994532\",\"867696065993088\",\"867696065992890\",\"867696065987528\",\"867696065989383\",\"867696065988898\",\"867696065991397\",\"867696065996115\",\"867696065992932\",\"867696065995455\",\"867696065992742\",\"867696065990266\",\"867696065992791\",\"867696065991835\",\"867696065989813\",\"867696065996263\",\"867696065990241\",\"867696065994193\",\"867696065990910\",\"867696065994946\",\"867696065990142\",\"867696065989730\",\"867696065987494\",\"867696065991314\",\"867696065987353\",\"867696065991181\",\"867696065987577\",\"867696065995950\",\"867696065994656\",\"867696065994565\",\"867696065991231\",\"867696065990225\",\"867696065992999\",\"867696065990993\",\"867696065995273\",\"867696065995265\",\"867696065994821\",\"867696065990399\",\"867696065996156\",\"867696065989714\",\"867696065989037\",\"867696065995844\",\"867696065993625\",\"867696065996180\",\"867696065314699\",\"867696065322262\",\"867696065316637\",\"867696065317510\",\"867696065313691\",\"867696065313311\",\"867696065995307\",\"867696065313907\",\"867696065988922\",\"867696065994425\",\"867696065991280\",\"867696065997535\",\"867696065314186\",\"867696065322247\",\"867696065987833\",\"867696065322064\",\"867696065987296\"],\"alarmType\":2,\"appType\":2,\"radius\":300,\"fenceId\":\"f5d49b6754a74366a83e514f38986f92\",\"longitude\":0.0}";
        JSONObject data = JSON.parseObject(s);
        AddOrgPolygonFenceDto addOrgPolygonFenceDto = JSONObject.toJavaObject(data, AddOrgPolygonFenceDto.class);
        System.out.println(addOrgPolygonFenceDto.toString());
    }
    /**
     * 增量设置圆形围栏
     * vcom 要求增加也可设置多边形围栏,后面接口全部兼容多边形围栏
     *
     * @param jsonObject
     * @param request
     * @return
     */
    @ApiOperation(value = "围栏绑定设备", notes = "")
    @PostMapping("/addOrgCircleFenceIncrement")
    public ResponseEntity addOrgCircleFenceIncrement(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        logger.info("addOrgCircleFenceIncrement param={}", jsonObject.toJSONString());
        JSONArray imeis = jsonObject.getJSONArray("imeis");
        List<String> imeiList = imeis.toJavaList(String.class);
        String token = request.getHeader(Constant.HEAD_TOKEN_KEY);
        Object validResult = validService.validImeiList(token, imeiList);
        if (validResult instanceof RespCode) {
            return new ResponseEntity((RespCode) validResult, false, null);
        }
        String fenceId = jsonObject.getString("fenceId");
        return vcomFenceService.incrementAddFenceImei(fenceId, (List<String>) validResult);
    }

    /**
     * 全量删除圆形围栏（根据fenceId）
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/deleteOrgCircleFence")
    public ResponseEntity deleteOrgCircleFence(@RequestBody JSONObject jsonObject) {
        logger.info("deleteOrgCircleFence param={}", jsonObject.toJSONString());
        String fenceId = jsonObject.getString("fenceId");
        return vcomFenceService.delFence(fenceId);
    }

    /**
     * 增量删除圆形围栏（根据）
     *
     * @param jsonObject
     * @param request
     * @return
     */
    @PostMapping("/removeOrgCircleFence")
    public ResponseEntity removeOrgCircleFence(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        logger.info("removeOrgCircleFence param={}", jsonObject.toJSONString());
        JSONArray imeis = jsonObject.getJSONArray("imeis");
        List<String> imeiList = imeis.toJavaList(String.class);
        String token = request.getHeader(Constant.HEAD_TOKEN_KEY);
        Object validResult = validService.validImeiList(token, imeiList);
        if (validResult instanceof RespCode) {
            return new ResponseEntity((RespCode) validResult, false, null);
        }
        String fenceId = jsonObject.getString("fenceId");
        return vcomFenceService.incrementDelFenceImei(fenceId, (List<String>) validResult);
    }

    /**
     * 获取机构围栏
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "获取机构围栏", notes = "")
    @PostMapping("/getOrgCircleFences")
    public RespEntity getOrgCircleFences(@RequestBody JSONObject jsonObject) {
        logger.info("getOrgCircleFences param={}", jsonObject.toJSONString());
        String fenceId = jsonObject.getString("fenceId");
        return vcomFenceService.getFenceInfo(fenceId);
    }


}
